package com.emapp.lccode.mode

import androidx.lifecycle.MutableLiveData
import com.emapp.lccode.base.BaseApplication
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.bean.Field
import com.emapp.lccode.bean.Project
import com.emapp.lccode.bean.SelectType
import com.emapp.lccode.bean.UpFileKt
import com.emapp.lccode.http.apiService
import com.emapp.lccode.utils.XtomLogger
import com.emapp.lccode.view.DeviceTypeDialog
import com.emapp.lccode.view.DistrictDialog
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class DeviceViewModel : BaseViewModel() {
    var device: MutableLiveData<ResultState<ArrayList<DeviceKt>>> = MutableLiveData()
    var devicelist: MutableLiveData<ResultState<DeviceKt>> = MutableLiveData()
    var deviceAdd: MutableLiveData<ResultState<Any>> = MutableLiveData()
    var project: MutableLiveData<ResultState<Project>> = MutableLiveData()
    var file: MutableLiveData<ResultState<UpFileKt>> = MutableLiveData()
    var fields: MutableLiveData<ResultState<ArrayList<Field>>> = MutableLiveData()
    var selectType: SelectType = SelectType()
    var selectDistrict: SelectType = SelectType()
    val typeDialog: DeviceTypeDialog by lazy { DeviceTypeDialog() }
    val districtDialog: DistrictDialog by lazy { DistrictDialog() }
    var device_id = ""
    var work_type = ""//设备大分类(水)
    private val TAG = this.javaClass.simpleName
    private fun log_e(msg: String?) {
        if (BaseConfig.isDebug) XtomLogger.e(TAG, msg)
    }
    /**
     * 设备属性
     */
    fun getDeviceField(
        device_type: String = "",
    ) {
        val u = BaseApplication.instance.getUser() ?: return
        request({
            apiService.getDeviceField(device_type)
        }, fields, true)
    }
    fun getDevice(
        check_status: String,
        page: Int
    ) {
        request(
            { apiService.getDeviceList(BaseApplication.instance.adminId,check_status,BaseConfig.page_count.toString(),page.toString()) },
            devicelist,
            false
        )
    }
    /**
     * 项目列表
     */
    fun getProjectList(
        keywords: String = "",
        year: String = "",
        city_ids: String = "",
        pager: Int = 0
    ) {
        val u = BaseApplication.instance.getUser() ?: return
        request({
            apiService.getProjectList(
                u.admin_id, keywords, year, city_ids, "80", "0"
            )
        }, project, true)
    }
    /**
     * 添加编辑设备
     */
    fun addDevice(infor: DeviceKt, map: Map<String, String>) {
        val u = BaseApplication.instance.getUser() ?: return
        infor.admin_id = u.admin_id
//        request({
//            apiService.deviceAdd(
//                infor.operation_type,
//                infor.id,
//                infor.work_type,
//                infor.device_type,
//                infor.project_id,
//                infor.name,
//                infor.num_code,
//                infor.device_status.toString(),
//                infor.upload_lnglat,
//                infor.address,
//                infor.username,
//                infor.phone,
//                infor.thumb,
//                infor.file_ids,
//                infor.province,
//                infor.city,
//                infor.district,
//                infor.boxcode,
//                infor.imei,
//                infor.changjia,
//                infor.year,
//                infor.buildtime,
//                infor.admin_id,
//                infor.long_len,
//                infor.depth,
//                infor.dn_norms,
//                infor.width_len,
//                infor.remark,
//                infor.rongliang,
//                infor.check_type,
//                infor.bianma_num,
//                infor.json_text,
//                infor.water_outlet_plug
//            )
        request({
            apiService.deviceAdd2(
                infor.operation_type,
                infor.id,
                infor.work_type,
                infor.device_type,
                infor.project_id,
                infor.name,
                infor.num_code,
                infor.device_status.toString(),
                infor.question_sta.toString(),
                infor.upload_lnglat,
                infor.address,
                infor.username,
                infor.phone,
                infor.thumb,
                infor.file_ids,
                infor.province,
                infor.city,
                infor.district,
                infor.changjia,
                infor.year,
                infor.buildtime,
                infor.remark,
                infor.admin_id,
                infor.json_text,
                infor.boxcode,
                infor.imei,
                map
            )
        }, deviceAdd, true)
    }

    fun upFileDevice(f: File) {
        val builder = MultipartBody.Builder().apply {
            setType(MultipartBody.FORM)
            addFormDataPart("admin_id", (BaseApplication.instance.getUser()?.admin_id ?: 0).toString())
            addFormDataPart("file", f.name, f.asRequestBody("image/*".toMediaTypeOrNull()))
        }
        request({ apiService.upFileDevice(builder.build()) }, file, false)
    }
    /**
     * 设备详情
     */
    fun getDeviceInfor(work_type: String, id: String) {
        request({
            apiService.getDeviceInfor(work_type, id)
        }, device, true)
    }
}